﻿using Microsoft.AspNetCore.Mvc;
using StudentCouncil.Model;
using StudentCouncil.Model.Models;
using StudentCouncil.Model.Parameter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StudentCouncil.DAL.DAO
{
   public class CounciDAO
    {
        public static Pager<Competition> GetListByPage(CompetitionParameter parameter)
        {
            Pager<Competition> result = new Pager<Competition>();
            using (StudentContext db = new StudentContext())
            {
                var query = db.Competition.AsQueryable();
                if (!string.IsNullOrEmpty(parameter.Bsname))
                {
                    query = query.Where(t => t.Bsname.Contains(parameter.Bsname));
                }

                result.Total = query.Count();
                result.Rows = query.OrderBy(t => t.CompetitionId)
                    .Skip(parameter.Skip)
                    .Take(parameter.PageSize)
                    .ToList();
                return result;
            }
        }

        public static Pager<Competitionstatus> GetstatusListByPage(CompetitionstatusParameter parameter)
        {
            Pager<Competitionstatus> result = new Pager<Competitionstatus>();
            using (StudentContext db = new StudentContext())
            {
                var query = db.Competitionstatus.AsQueryable();
                if (!string.IsNullOrEmpty(parameter.Bsname))
                {
                    query = query.Where(t => t.Bsname.Contains(parameter.Bsname));
                }

                result.Total = query.Count();
                result.Rows = query.OrderBy(t => t.CompetitionId)
                    .Skip(parameter.Skip)
                    .Take(parameter.PageSize)
                    .ToList();
                return result;
            }
        }
        public static bool DeleteById(int id)
        {
            using (StudentContext db = new StudentContext())
            {
                var entity = db.Competition.Find(id);
                if (null != entity)
                {
                    db.Competition.Remove(entity);
                    db.SaveChanges();
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        public static bool BatchDelete(string[] ids)
        {
            using (StudentContext db = new StudentContext())
            {
                var list = db.Competition.Where(t => ids.Contains(t.CompetitionId.ToString()));
                db.Competition.RemoveRange(list);
                db.SaveChanges();
                return true;
            }
        }

        public static bool AddCompetition([FromBody]Competition entity)
        {

            using (StudentContext db = new StudentContext())
            {

                db.Competition.Add(entity);
                db.SaveChanges();
                return true;
            }
        }
        public static Competition GetCompetitionById(int id)
        {
            using (StudentContext db = new StudentContext())
            {
                var res = db.Competition.Find(id);
                return res;
            }
        }
        public static bool Update(Competition entity)
        {
            using (StudentContext db = new StudentContext())
            {
                var dbEntity = db.Competition.Find(entity.CompetitionId);
                if (null == dbEntity)
                {
                    return false;
                }
                else
                {
                    dbEntity.CompetitionId = entity.CompetitionId;
                    dbEntity.Bsname = entity.Bsname;
                    dbEntity.Bssponsor = entity.Bssponsor;
                    dbEntity.Bstime = entity.Bstime;
                    dbEntity.Bsaddress = entity.Bsaddress;
                    dbEntity.Bsmethod = entity.Bsmethod;
                    db.SaveChanges();
                    return true;
                }
            }
        }
        public static bool UpdateCompetitionstatus(Competitionstatus entity)
        {
            using (StudentContext db = new StudentContext())
            {
                var data = db.Competitionstatus.First(t => t.CompetitionId == entity.CompetitionId);
                data.Status = entity.Status;
                db.SaveChanges();
                return true;
            }
        }

        public static bool AddCompetitionstatus([FromBody]Competitionstatus entity)
        {

            using (StudentContext db = new StudentContext())
            {

                db.Competitionstatus.Add(entity);
                db.SaveChanges();
                return true;
            }
        }
    }
}
